// pages/appointmentTime/appointmentTime.js
import api from "../../../utils/util.js"; // api 请求接口
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navmess:{
      navtitle: "选择时间",
      backhide: 0,
      topheight: 1
    },
	dateTimeList: {}, // 预约时间存放对象
    Datelist: [], // 日期，星期列表
    dataIdx: 0, // 0是今天
    activeIdx: 0,// 选中日期的下标
    activeDateObj: {normal: '',shortTime: '',week: ''}, // 选中的日期
    mIdx: '', //上午时间的下标
    aIdx: '',// 下午时间的下标
    eIdx: '', // 晚上时间的下标
    type: '',
    activeTime: '',// 选中时间
    intervalCode: 0, // 时间对应的编号 列：8:00=>1 2 3 ...
    commAndBea: {}, // 项目、美容师 存放
    BeauticianInfo: {}, // 美容师的信息
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let commAndBeaInfo = wx.getStorageSync('commAndBea');
    this.setData({commAndBea: commAndBeaInfo, BeauticianInfo: commAndBeaInfo.appointStaffId});
    console.log("拿到的数据：",commAndBeaInfo);
    this.startCalc(0);
	  this.getAppointment(); // 获取可预约时间
  },
  // 下一步
  next: function(){
    if(this.data.activeTime==""){this.toast("时间还没选择哦");return;}
   /*
    * 1.预约日期和时间 2.修改预约还是正常流程预约标记 3.预约的美容师 4.预约的项目
    * 
    */
    this.data.commAndBea.date = {norma: this.data.activeDateObj, time:this.data.activeTime, intervalCode: this.data.intervalCode};
    console.log(this.data.commAndBea, this.data.activeDateObj, this.data.activeTime);
    wx.setStorageSync('appTimeNextInfo', this.data.commAndBea);
    wx.navigateTo({url: '/pages/orders/appointmenTinfoConfirm/appointmenTinfoConfirm'});
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 获取可否可预约状态
  getAppointment: function(){
    let date = this.data.activeDateObj.normal;
	  api.post('/mini/appoint/appointTime',{storeId: 1, appointTime: date}).then(res=>{
		  let resTime = res.result,dateTimeList = this.data.dateTimeList; 
		  dateTimeList.morning = resTime.morning; // 早上
		  dateTimeList.noon = resTime.noon; // 中午
		  dateTimeList.night = resTime.night;// 晚上
		  this.setData({dateTimeList: dateTimeList})
		  
		  console.log("获取返回的状态：",res);
	  })
  },
  // 0上(按钮) 5天 1下(按钮) 五天
  getFiveDays: function(e){
    let btn = e.currentTarget.dataset.btn,
        Idx = this.data.dataIdx; // 左边是否到底了=0到底
    if(Idx==0 && btn==0)return;
    if(btn == 0){ //上五天
      this.setData({dataIdx: this.data.dataIdx-1})
      this.startCalc(1);
    }else if(btn == 1){ // 后五天
      this.setData({dataIdx: this.data.dataIdx+1})
      this.startCalc(2);
    }
  },
  // 获取选择的日期
  getDate: function(e){
    var dataset = e.currentTarget.dataset,
        idx = dataset.index;
        this.setData({activeIdx: idx}); // 选择的日期
        this.getDATEformat();
  },
  // 获取选择的时间
  getTime: function(e){
    var database = e.target.dataset,Idx=database.index,activeTime=database.time,
        type = database.type, // 上午 下午 晚上
        item = database.item, // 每一个时间段的对象
        status = item.status;// 是否可约状态 0=>预满 1可约 .
        this.setData({intervalCode: item.intervalCode});
	  if(status == 0){this.toast(item.pointName+' 已约满'); return;}
	  this.setData({Idx: Idx, type: type});
    this.setData({activeTime: activeTime});//时间
  },
  // 获取日期时间
  getDay: function (startDate, day){
    var today = startDate==undefined || startDate==""? new Date():new Date(startDate);
    var targetday_milliseconds=today.getTime() + 1000*60*60*24*day;
    today.setTime(targetday_milliseconds); //注意，这行是关键代码
    var tYear = today.getFullYear();
    var tMonth = today.getMonth();
    var tDate = today.getDate();
    var Day = today.getDay();
    tMonth = this.doHandleMonth(tMonth + 1);
    tDate = this.doHandleMonth(tDate);
    return tYear+"-"+tMonth+"-"+tDate+"/"+Day;
  },
  doHandleMonth: function (month){
    var m = month;
    if(month.toString().length == 1){
     m = "0" + month;
    }
    return m;
  },
  startCalc: function(type){ // 0 默认今天开始+5天 1=>上五天 2=>下五天
    let Datelist = this.data.Datelist,
        strat=type==0?'':type==1?Datelist[0].normalDate:Datelist[5].normalDate;
    let Week = new Array("周日", "周一", "周二", "周三", "周四", "周五", "周六");
    this.setData({Datelist: []});
    if(type == 0 || type == 2){
      for(var i=0; i<=5; i++){
        let date = this.getDay(strat,i).split("/");
        let temp = {
          date: date[0].slice(5),
          normalDate: date[0],
          week: Week[date[1]]
        }
        this.data.Datelist.push(temp);
      }
    }else{
      for(var i=0; i>=-5; --i){
        let date = this.getDay(strat,i).split("/");
        let temp = {
          date: date[0].slice(5),
          normalDate: date[0],
          week: Week[date[1]]
        }
        this.data.Datelist.push(temp);
      }
    }
  
    this.setData({Datelist: type==1?this.data.Datelist.reverse():this.data.Datelist})
    this.getDATEformat();
  },
  // 时间日期组合
  getDATEformat: function(){
    let Datelist = this.data.Datelist[this.data.activeIdx],
        activeDateObj = {normal: Datelist.normalDate,shortTime: Datelist.date,week: Datelist.week};
    this.setData({activeDateObj: activeDateObj});
  },

  // 公共toast
  toast: function(title, icon){
	wx.showToast({title: title, icon: icon==undefined||icon==''? 'none':icon});
  }
})